﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormHumidity : Form
    {
        private bool change = false;

        public FormHumidity()
        {
            InitializeComponent();
        }

        private void dataGridViewZoneColumnRefresh()
        {
            //为地区列赋值
            for (int i = 0; i < tb_humidityDataGridView.Rows.Count; i++)
            {
                if (tb_humidityDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn2.Name].Value != null)
                {
                    int mon_id = Convert.ToInt32(tb_humidityDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn2.Name].Value.ToString());

                    // 获取当前行监控箱号
                    IEnumerable<tb_monitor_device> qZone =
                    from mon in Monitor_DB.instance.db.tb_monitor_device
                    where mon.ID == mon_id
                    select mon;

                    foreach (var item in qZone)
                    {
                        tb_humidityDataGridView.Rows[i].Cells[zone.Name].Value = item.Zone;
                    }
                }
            }
        }

        private void tb_humidityBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            DataTable daorig = new DataTable();
            DataTable dadest = new DataTable();

            int[] check = new int[9] { 0, 0, 0, 0, 0, 0, 0, 0, 1 };

            try
            {
                SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlDataAdapter sda2 = new SqlDataAdapter();


                string strSql = "select * from tb_humidity";
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(daorig);
                tb_humidityDataGridView.EndEdit();
                Monitor_DB.instance.db.SubmitChanges();
                change = false;

                sda.Fill(dadest);

                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.humidity, "配置湿度传感器", "成功", check);

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception Ex)
            {
                UserLog.addrecordfordatagridview(daorig, dadest, UserLog.humidity, "配置湿度传感器", "失败", check);
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }
        }

        private void FormHumidity_Load(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.db.Refresh(RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_humidity);
                this.tb_humidityBindingSource.DataSource = Monitor_DB.instance.db.tb_humidity;

                // 监控箱号
                var query =
                    from mon in Monitor_DB.instance.db.tb_monitor_device
                    select mon;
                foreach (var item in query)
                {
                    dataGridViewTextBoxColumn2.Items.Add(item.ID);
                }

                // 刷新地区列
                dataGridViewZoneColumnRefresh();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void tb_humidityDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tb_humidityDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 刷新地区列
            dataGridViewZoneColumnRefresh();
        }

        private void tb_humidityDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_humidityBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void FormHumidity_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (change)
            {
                DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Yes)
                {
                    tb_humidityBindingNavigatorSaveItem_Click(sender, e);
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
        }

        private void tb_humidityDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            change = true;
        }

        private void nameTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void thresholdTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void threshold_TwoTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void threshold_ThreeTextBox_Click(object sender, EventArgs e)
        {
            change = true;
        }

        private void inUseCheckBox_Click(object sender, EventArgs e)
        {
            change = true;
        }
    }
}
